/******************************************************************************
 * 
 * File Name	: bus_i2c.h
 *
 * Version		: V1.0
 *
 * Date		    : 
 *
 * Author		: 
 *
 * Note: this file originally from project 
 *
 *****************************************************************************/

#ifndef _BUS_I2C_H
#define _BUS_I2C_H

#include <stdio.h>
#include <math.h>
#include "gd32f20x.h"


#define SENSOR_CONVERT_RAW_VAL(Y, M, B, K1, K2) (((Y / pow(10, K2)) - (B * pow(10, K1))) / M)

#define SENSOR_TEMPO_M_MULTI_VAL    1
#define SENSOR_TEMPO_B_OFFSET_VAL   (-64)
#define SENSOR_TEMPO_K1_BEXP_VAL    0
#define SENSOR_TEMPO_K2_REXP_VAL    0

#define SENSOR_12V_M_MULTI_VAL      2
#define SENSOR_12V_B_OFFSET_VAL     0
#define SENSOR_12V_K1_BEXP_VAL      0
#define SENSOR_12V_K2_REXP_VAL      (-1)

#define SENSOR_12VA_M_MULTI_VAL     5
#define SENSOR_12VA_B_OFFSET_VAL    0
#define SENSOR_12VA_K1_BEXP_VAL     0
#define SENSOR_12VA_K2_REXP_VAL     (-1)

#define FAN_SPEED_M_MULTI_VAL       50
#define FAN_SPEED_B_OFFSET_VAL      0
#define FAN_SPEED_K1_BEXP_VAL       0
#define FAN_SPEED_K2_REXP_VAL       0

#pragma pack(1)

typedef struct {
	// uint8_t target_addr;// 1Byte	Ŀ����?
	uint8_t NetFn;// 1Byte NetFn����
				  // [2-7]:NetFn
				  // [0-1]:LUN
	uint8_t head_check_sum;// 1Byte	ͷ��У���?
	uint8_t src_addr;// 1Byte Դ��ַ
	uint8_t req_num;// 1Byte �������к�
	uint8_t cmd_code;// 1Byte ������
	uint8_t state;// 1Byte	���״�?
	uint8_t reply[24];// 0~24 Byte Ӧ�����ݶ�
	uint8_t check_sum;// 1Byte У���?
} REPORT_DATA;

#pragma pack()

#pragma pack(1)

typedef struct {
	uint8_t board_slot;
	uint8_t slave_add;
	uint8_t poweron;	// 1-on,0-off
	uint8_t online;
	
	uint16_t sensor_temp[5];
	uint16_t sensor_V33SV; 
	uint16_t sensor_I33SV;
	uint16_t sensor_P33SV;
	
	uint16_t sensor_V33V; 
	uint16_t sensor_I33V;
	uint16_t sensor_P33V;

	uint16_t sensor_V5V; 
	uint16_t sensor_I5V; 
	uint16_t sensor_P5V;
	
	uint16_t sensor_V12V; 
	uint16_t sensor_I12V; 
	uint16_t sensor_P12V;

	uint16_t sensor_VB1V; 
	uint16_t sensor_IB1V; 
	uint16_t sensor_PB1V;

	uint16_t sensor_VB2V; 
	uint16_t sensor_IB2V; 
	uint16_t sensor_PB2V;

	uint16_t fan_speed;

} BUS_DATA;

#pragma pack()

extern BUS_DATA ipmi_data;

int bus_start(uint32_t i2c_periph);
uint32_t Get_Fan_Speed(void);
void i2cbus_thread_entry(void *arg);

#endif //_BUS_I2C_H

